<!doctype html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<script>
    const r = {
        init(size) {
            this.ic = []
            for (let i = 0; i < size; i++) {
                this.ic[i] = i
            }
        },

        checkInit() {
            if (this.ic == null) {
                throw "没有初始化"
            }
        },

        randomPop() {
            this.checkInit()
            if (this.ic.length === 0) {
                return -1
            }
            let i = Math.floor(Math.random() * this.ic.length)
            const res = this.ic[i];
            this.ic.splice(i, 1)
            return res
        },

        pop() {
            this.checkInit()
            if (this.ic.length === 0) {
                return -1
            }
            return this.ic.shift()
        },
    }
    r.init(100)
    for (let i = 0; i < 120; i++) {
        if (Math.random() > .5) {
            console.log(r.randomPop());
        } else {
            console.log(r.pop())
        }
    }
</script>
</body>
</html>